package com.client.service;

import com.client.bean.CollectionSoftware;
import com.client.bean.FeedBack;
import com.client.bean.Report;
import com.client.dbdao.SoftwareDetailClientDao;
import com.data.util.CheckDataUtils;
import com.data.util.DatabaseIdUtils;
import com.data.util.FinalStringUtils;
import com.data.util.ImageLookUtils;
import com.user.bean.Speaking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 二级界面服务层处理
 *
 * @author:HingLo
 * @create 2017-10-10 15:52
 **/
@Service
public class SoftwareDetailClientService {

    @Autowired
    private SoftwareDetailClientDao softwareDetailClientDao;


    public List<?> findSoftwareDetailService(String id) {

        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            return softwareDetailClientDao.findSoftwareDetailDao(id, "bigImage");
        }
        return null;
    }

    public byte[] lookBigImageService(String rid, String name) {
        rid = rid.trim();
        name = name.trim();
        return ImageLookUtils.lookImageNameUtils(rid, name);
    }

    /**
     * 评论，软件的id
     *
     * @param id
     * @return
     */
    public List<?> lookSpeakInfoService(String id, int start, int end) {

        if (start < 0) {
            start = 0;
        }

        if (end < 0) {
            end = 2;
        }

        if (end < start) {
            start = 0;
            end = 2;
        }
        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            return softwareDetailClientDao.lookSpeakInfoDao(id, start, end);
        }
        return null;

    }

    /**
     * 给指定的用户评论点赞
     *
     * @param id
     * @return
     */
    public boolean addSpeakingGoodService(String id) {
        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            return softwareDetailClientDao.updateSpeakingGoodDao(id);
        }
        return false;
    }

    /**
     * 提交评论信息
     *
     * @param speaking
     * @return
     */
    public boolean addSpeakingSoftwareService(Speaking speaking) {
        if (speaking != null && CheckDataUtils.stringUtilsID(speaking.getUid(), speaking.getRid()) &&
                CheckDataUtils.stringUtils(speaking.getContents())) {
            if (speaking.getStar() > 5 || speaking.getStar() < 0) {
                return false;
            }

            speaking.setId(DatabaseIdUtils.getDataIdOne());
            speaking.setCtime(new Date());
            return softwareDetailClientDao.saveMassageDao(speaking);
        }
        return false;
    }

    /**
     * 反馈软件
     *
     * @param feedBack
     * @return
     */

    public boolean feedBackSoftwareService(FeedBack feedBack) {
        feedBack.setId(DatabaseIdUtils.getDataIdOne());
        feedBack.setCtime(new Date());
        feedBack.setState(FinalStringUtils.STATE_DEFAULT);
        return softwareDetailClientDao.saveMassageDao(feedBack);
    }

    /**
     * 举报软件
     *
     * @param report
     * @return
     */
    public boolean reportSoftwareService(Report report) {
        //检测举报人的id是否正确，与信息是否正确
        if (CheckDataUtils.stringUtilsID(report.getRid()) && CheckDataUtils.stringUtils(report.getType())) {

            //去掉信息
            String[] s = report.getType().split(",");
            StringBuilder stringBuilder = new StringBuilder();
            for (String s1 : s) {
                if (!s1.equals("")) {
                    stringBuilder.append(s1 + ",");
                }
            }
            report.setType(Arrays.toString(stringBuilder.toString().split(",")));

            report.setId(DatabaseIdUtils.getDataIdOne());
            report.setCtime(new Date());
            report.setState(FinalStringUtils.STATE_DEFAULT);
            return softwareDetailClientDao.saveMassageDao(report);
        }
        return false;


    }

    /**
     * 添加收藏软件
     *
     * @param uid
     * @param rid
     * @return
     */
    public boolean CollectionSoftwareService(String uid, String rid) {
        rid = rid.trim();
        uid = uid.trim();
        CollectionSoftware collectionSoftware = new CollectionSoftware();
        collectionSoftware.setRid(rid);
        collectionSoftware.setUid(uid);
        collectionSoftware.setCtime(new Date());
        return softwareDetailClientDao.saveMassageDao(collectionSoftware);
    }

    public List<?> findCollectionSoftwareService(String uid) {
        if (CheckDataUtils.stringUtilsID(uid)) {
            uid = uid.trim();
            return softwareDetailClientDao.findCollectionSoftwareDao(uid);

        }
        return null;
    }

    public List<?> searchSoftwareService(String search) {

        if (CheckDataUtils.stringUtils(search)) {
            search = search.trim().toLowerCase();
            return softwareDetailClientDao.searchSoftwareDao(search);
        }
        return null;
    }

    public boolean findCollectionSoftwareService(String uid, String rid) {
        if (CheckDataUtils.stringUtilsID(uid, rid)) {
            rid = rid.trim();
            uid = uid.trim();
            List list = softwareDetailClientDao.findCollectionSoftwareDao(uid, rid);
            if (!list.isEmpty())
                return true;
        }
        return false;
    }

    /**
     * 搜索标签页面
     *
     * @return
     */
    public List<?> searchTagsSoftwareService(int len) {

        return softwareDetailClientDao.searchTagsSoftwareDao(len);
    }

    public boolean deleteCollectionSoftwareService(String uid, String rid) {
        String[] rids = rid.split(",");
        return softwareDetailClientDao.deleteCollectionSoftwareDao(uid, rids);
    }


    public List<?> updateAdminSoftwareService(String packageName, boolean bool) {
        if (CheckDataUtils.stringUtils(packageName)) {

            String packageNames[] = packageName.split(",");
            return softwareDetailClientDao.updateAdminSoftwareDao(packageNames, bool);

        }
        return null;
    }


    public List<?> speakLookService(String rid) {
        if (CheckDataUtils.stringUtilsID(rid)) {
            return softwareDetailClientDao.speakLookDao(rid);
        }
        return new ArrayList<>();
    }

    public List<?> searchTagSoftwareService(String search) {
        if (CheckDataUtils.stringUtils(search)) {
            return softwareDetailClientDao.searchTagSoftwareDao(search);
        }
        return null;
    }

    /**
     * 软件详情页面的搜索推荐
     *
     * @param len
     * @return
     */
    public List<?> recommendSoftwareService(String id, int len) {
        return softwareDetailClientDao.recommendSoftwareDao(id, len);
    }
}